#!/usr/bin/env python
import sys
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
import re
sys.setdefaultencoding('utf-8')

#mysql
def set_open_conn():    
    conn = HandleMysql('10.3.10.50','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_groupbuy_open')
    conn.connect()
    return conn

#mysql - stats
def set_stats_conn():    
    conn = HandleMysql('10.3.10.50','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_groupbuy_stats')
    conn.connect()
    return conn

#
def set_mid_data( date ):
    
    print "set_mid_data bigin : %s"%date
    
    conn = set_open_conn()
    stat_conn = set_stats_conn()    
    stat_table = "t_prodtype_stats"
    
    #query = "truncate table t_prodtype_stats"
    #conn.execute_sql(query)
    
    query = "select code from rs_page"
    items = conn.get_results(query)
    
    for item in items:
        code = item[0]
        print code
        
    print "set_mid_data end : %s"%date
     
def execute( date):
    print date
    print set_mid_data(date)

#
def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()